import React, { forwardRef, useImperativeHandle, memo } from 'react'
import { Pagination, Table } from 'antd'
import type { PaginationProps } from 'antd'
import { uid } from 'uid'
import './index.scss'
const showTotal: PaginationProps['showTotal'] = total => `共${total}条`
const LdPrintTable = memo(forwardRef((props: any, ref: any) => {
    const { bordered = true, handlePage, selectRowChange, rowSelection = true, rowSelectionType = 'checkbox', columns = [], data = [], showPage = true, pageConfig = { currentPage: 1, total: 0, pageSize: 10 } } = props
    const rowSelectionFn = {
        onChange: (selectedRowKeys: Array<any>, selectedRows: Array<any>) => {
            selectRowChange && selectRowChange(selectedRows)
        }
    }
    const onChange = (currentPage: number, pageSize: number) => {
        const params = {
            ...pageConfig,
            currentPage,
            pageSize
        }
        handlePage && handlePage(params)
    }
    useImperativeHandle(ref, () => ({

    }))
    return (
        <>
            {/* rowSelection={rowSelection ? { type: rowSelectionType, ...rowSelectionFn } : {}} */}
            <div className='ld-table'>
                <Table
                    style={{height:`${showPage ? 'calc(100% - 50px)' : '100%'}`}}
                    rowKey={record => record.id || record.identify || record.uid}
                    rowSelection={rowSelection ? { type: rowSelectionType, ...rowSelectionFn } : undefined}
                    bordered={bordered}
                    sticky={true}
                    pagination={false}
                    columns={[{ key: 'index', dataIndex: 'index', title: '序号', width: 50, render: (_: any, record: any, index: number) => index + 1 }, ...columns]}
                    dataSource={data}>
                </Table>
                {showPage ?
                    <Pagination
                        showTotal={showTotal}
                        defaultCurrent={1}
                        current={pageConfig.currentPage}
                        defaultPageSize={pageConfig.pageSize}
                        total={pageConfig.total} onChange={onChange}></Pagination>
                    : null}
            </div>
        </>
    )
}))
export default LdPrintTable